

#import "SDTabBarController.h"
#include "SDAppConfigManager.h"
#import "AAPLCameraViewController.h"

@interface SDTabBarController ()<VPTabBarDelegate>

@end

@implementation SDTabBarController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self removeSystemBar];
    [self initTabBar];
    
    [self performSelector:@selector(selectBootTabIndex) withObject:nil afterDelay:0.2];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
   }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)selectBootTabIndex
{
    int bootTabIndex = [[SDAppConfigManager sharedInstance] getBootTabSelectedIndex];
    [_myTabBar selectTabAtIndex:bootTabIndex];
    [self selectBarBtnAtIndex:bootTabIndex];
}

-(void) dealloc
{
}

#pragma mark ---

-(VPTBarBtn*) buttonOfIndex:(NSInteger) index
{
    if(index<_myTabBar.btnList.count && index>=0)
        return (VPTBarBtn*)[_myTabBar.btnList objectAtIndex:index];
    
    return nil;
}

-(void)removeSystemBar
{
    if (self.view.subviews.count >=2)
    {
        UIView *transitionView = [self.view.subviews objectAtIndex:0];
        transitionView.frame = CGRectZero;
        
        UIView *systemBarView = [self.view.subviews objectAtIndex:1];
        systemBarView.hidden = YES;
    }
}

-(void) initTabBar
{
    CGFloat tabY = kScreenHeight - MainTabBar_H;
    
    VPTBarBtn* one = [[VPTBarBtn alloc] init];
    one.normalImg = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"btm_actionbar_btn_picture.png");
    one.selectImg = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"btm_actionbar_btn_picture_h.png");
    one.normalColor = [UIColor colorWithRGB:0x747F8B];
    one.selectColor = [UIColor colorWithRGB:0x747F8B];
    one.rightTitle = @"照片";
    
    VPTBarBtn* two = [[VPTBarBtn alloc] init];
    two.normalImg = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"btm_actionbar_btn_camera.png");
    two.selectImg = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"btm_actionbar_btn_camera_h.png");
    two.normalColor = [UIColor colorWithRGB:0x747F8B];
    two.selectColor = [UIColor colorWithRGB:0x747F8B];
    two.rightTitle = @"拍照";
    
    VPTBarBtn* three = [[VPTBarBtn alloc] init];
    three.normalImg = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"btm_actionbar_btn_patient.png");
    three.selectImg = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"btm_actionbar_btn_patient_h.png");
    three.normalColor = [UIColor colorWithRGB:0x747F8B];
    three.selectColor = [UIColor colorWithRGB:0x747F8B];
    three.rightTitle = @"患者";
    
    VPTBarBtn* four = [[VPTBarBtn alloc] init];
    four.normalImg = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"btm_actionbar_btn_setting.png");
    four.selectImg = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"btm_actionbar_btn_setting_h.png");
    four.normalColor = [UIColor colorWithRGB:0x747F8B];
    four.selectColor = [UIColor colorWithRGB:0x747F8B];
    four.rightTitle = @"设置";
    
    _myTabBar = [[VPTabBar alloc] initWithPosY:tabY btnList:[NSArray arrayWithObjects:one, two, three,four, nil]];
    _myTabBar.mydelegate = self;
    _myTabBar.backgroundColor = [UIColor colorWithRGB:0xffffff];
    [_myTabBar initMyContent];
    
    [self.view addSubview:_myTabBar];

}

-(void)resetMyTabBarPositon
{
     CGFloat tabY = kScreenHeight - MainTabBar_H;
     _myTabBar.top = tabY;
     _myTabBar.alpha = 1;
}
#pragma mark ---
#pragma mark VPTabBarDelegate <NSObject>

-(void) selectBarBtnAtIndex:(NSInteger) index
{
    [self setSelectedIndex:index];
    // 清除对比照片和对比tag
    UIViewController* viewController = self.selectedViewController;
    if ([viewController isKindOfClass:[AAPLCameraViewController class]]) {
        AAPLCameraViewController* cameraVC = (AAPLCameraViewController*)viewController;
        cameraVC.compareImgeName = nil;
        cameraVC.comparePhotoInfo = nil;
        [cameraVC setCompareImage];
    }
}

-(void) doubleClickedAtIndex:(NSInteger) index
{
    SDBaseViewController* selCtrler = (SDBaseViewController*)(self.selectedViewController);
    if ([selCtrler respondsToSelector:@selector(simulationRefreshOfPullDown)])
    {
        [selCtrler simulationRefreshOfPullDown];
    }
}

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_5_1

- (BOOL) shouldAutorotate NS_AVAILABLE_IOS(6_0)
{
    return [self.selectedViewController shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0)
{
    return [self.selectedViewController supportedInterfaceOrientations];
}

#endif

-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    
}
@end
